﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Baosight.Platform.Core.UI.Controls;
using Baosight.ZNH.ZMCA.UI.Utils;
using Telerik.Windows.Controls.Gauges;
using Baosight.ZNH.ZMCA.UI.Controls;
using Baosight.Platform.Core.ServiceModel;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon;

using Telerik.Windows.Controls;
using Telerik.Windows.Controls.Charting;
using Baosight.ZNH.ZMCA.UI.Model;


namespace Baosight.ZNH.ZMCA.UI
{
    public partial class ZMCA80 : EFViewCmes
    {
        ImageSource imgGetMoreSource = Common.GetImageSource("/Images/GetMore.jpg");
        ImageSource imgGetMoreUnSelectedSource = Common.GetImageSource("/Images/GetMoreUnSelected.jpg");
        ImageSource imgFoldGetMoreSource = Common.GetImageSource("/Images/FoldGetMore.jpg");
        ImageSource imgFoldGetMoreUnSelectedSource = Common.GetImageSource("/Images/FoldGetMoreUnSelected.jpg");

        public ZMCA80()
        {
            InitializeComponent();

            //先将图片soure设置成选中再设置成未选中，是为了将图片加载进来，否则系统会延迟加载，即会在用户鼠标移到操作图片上之后再加载选中状态的图片，界面会有跳跃感。
            //imgGetMore.Source = imgGetMoreSource;
            //imgFoldGetMore.Source = imgFoldGetMoreSource;
            this.Loaded += new RoutedEventHandler(ZMCA00_Loaded);

        }

        void ZMCA00_Loaded(object sender, RoutedEventArgs e)
        {
            //imgGetMore.Source = imgGetMoreUnSelectedSource;
            //imgFoldGetMore.Source = imgFoldGetMoreUnSelectedSource;
        }

        public override void Initialize(EIInfo initData)
        {
            base.Initialize(initData);
            ZMCA03Controler.InitWarningPassRate(initData);

            LoadGaugeData(initData);
            RegEventForGauges();
            LoadIssueData();
        }


        /// <summary>
        /// 对于用户指定的指标，前四个显示在默认的LayoutBasicIndex表格中，其他的显示在LayoutMoreIndex表格中
        /// 每行显示3个
        /// </summary>
        /// <param name="initData"></param>
        private void LoadGaugeData(EIInfo initData)
        {
            EIBlock indexBlock = initData.GetBlock("index");
            for (int i = 0; i < indexBlock.Rows.Count && i < 6; i++)
            {
                EIRow index = indexBlock.Rows[i];
                string indexName = index["fdDetailsName"].ToString();
                UserControl c = GetGaugeByName(indexName);
                c.Margin = new Thickness(10, 0, 10, 5);
                if (c != null)
                {
                    int column = i % 3;
                    int row = i / 3 + i / 3;
                    c.SetValue(Grid.ColumnProperty, column);
                    c.SetValue(Grid.RowProperty, row);
                    LayoutBasicIndex.Children.Add(c);
                }
            }

            if (indexBlock.Rows.Count > 6)
            {
                //假设用户选了10个指标，则LayoutMoreIndex里面应该包括2行：(10-6+2)/3 = 2;
                //假设用户选了7个指标，则LayoutMoreIndex里面应该包括1行：(7-4+2)/3 = 1;
                int rowNumber = (indexBlock.Rows.Count - 6 + 2) / 3;
                for (int i = 0; i < rowNumber; i++)
                {
                    RowDefinition cd = new RowDefinition();
                    cd.Height = new GridLength(188, GridUnitType.Pixel);
                    LayoutMoreIndex.RowDefinitions.Add(cd);
                }
                for (int i = 6; i < indexBlock.Rows.Count; i++)
                {
                    EIRow index = indexBlock.Rows[i];
                    string indexName = index["fdDetailsName"].ToString();
                    UserControl c = GetGaugeByName(indexName);
                    if (c != null)
                    {
                        int column = (i - 6) % 3;
                        int row = (i - 6) / 3;
                        c.SetValue(Grid.ColumnProperty, column);
                        c.SetValue(Grid.RowProperty, row);
                        LayoutMoreIndex.Children.Add(c);
                    }
                }

            }
        }

        //回采工作面效率
        //原煤全员效率
        //原煤产量
        //综合进尺
        //开拓进尺
        //安全隐患
        //井下三违
        //材料消耗
        private UserControl GetGaugeByName(string indexName)
        {
            UserControl c = null;
            double canvasHeight = 230;
            double chartHeight = 188;
            switch (indexName)
            {
                case "工效":
                    {
                        ZMCA06Controler controler = new ZMCA06Controler();
                        DoubleCircleGauge gauge = new DoubleCircleGauge();
                        controler.GetGaugeData(gauge);
                        gauge.gaugeTitle.Text = "工    效       ";
                        c = gauge;
                        break;
                    }
                case "安全":
                    {
                        ZMCA15Controler controler = new ZMCA15Controler();
                        DoubleCircleGauge gauge = new DoubleCircleGauge();
                        controler.GetGaugeData(gauge);
                        gauge.gaugeTitle.Text = "安    全       ";
                        c = gauge;
                        break;
                    }
                case "材料":
                    {
                        ZMCA84Controler controler = new ZMCA84Controler();
                        DoubleCircleGauge gauge = new DoubleCircleGauge();
                        gauge.Tag = "ZMCA84";
                        gauge.gaugeTitle.Text = "材    料       ";
                        controler.GetGaugeData(gauge);
                        c = gauge;
                        break;
                    }
                case "采煤工程进度":
                    {
                        UserControl con = new UserControl();
                        con.Tag = "ZMCA03";

                        Canvas canva = new Canvas() { Height = canvasHeight, Width = 370 };
                        ImageBrush imageBrush = new ImageBrush();
                        imageBrush.ImageSource = new BitmapImage(new Uri("Images/GaugeBack.png", UriKind.Relative));
                        canva.Background = imageBrush;

                        ZMCA03Controler controler = new ZMCA03Controler();
                        controler.PlanType = "采煤计划";
                        RadChart engChart = new RadChart() { Height = chartHeight, Width = 340 };
                        //controler.GetEngineeringChartData(engChart, "0");
                        controler.GetChartData(engChart);

                        Canvas.SetLeft(engChart, 10);
                        Canvas.SetTop(engChart, 35);
                        canva.Children.Add(engChart);


                        TextBlock title = new TextBlock()
                        {
                            Height = 22,
                            Width = 370,
                            TextAlignment = TextAlignment.Center,
                            FontSize = 16,
                            FontStyle = FontStyles.Normal,
                            FontWeight= FontWeights.Bold
                        };

                        title.Text = "采煤工程进度    ";
                        Canvas.SetLeft(title, 0);
                        Canvas.SetTop(title, 3);
                        canva.Children.Add(title);
                        //con.Content = engChart;
                        con.Content = canva;

                        c = con;
                        break;
                    }
                case "掘进工程进度":
                    {
                        UserControl con = new UserControl();
                        con.Tag = "ZMCA74";

                        Canvas canva = new Canvas() { Height = canvasHeight, Width = 370 };
                        ImageBrush imageBrush = new ImageBrush();
                        imageBrush.ImageSource = new BitmapImage(new Uri("Images/GaugeBack.png", UriKind.Relative));
                        canva.Background = imageBrush;

                        ZMCA03Controler controler = new ZMCA03Controler();
                        controler.PlanType = "掘进计划";
                        RadChart engChart = new RadChart() { Height = chartHeight, Width = 340 };
                        //controler.GetEngineeringChartData(engChart, "1");
                        controler.GetChartData(engChart);

                        Canvas.SetLeft(engChart, 10);
                        Canvas.SetTop(engChart, 35);
                        canva.Children.Add(engChart);

                        TextBlock title = new TextBlock()
                        {
                            Height = 22,
                            Width = 370,
                            TextAlignment = TextAlignment.Center,
                            FontSize = 16,
                            FontStyle = FontStyles.Normal,
                            FontWeight = FontWeights.Bold
                        };

                        title.Text = "掘进工程进度    ";
                        Canvas.SetLeft(title, 0);
                        Canvas.SetTop(title, 3);
                        canva.Children.Add(title);

                        //con.Content = engChart;
                        con.Content = canva;

                        c = con;

                        break;
                    }
                case "防突打钻进度":
                    {
                        UserControl con = new UserControl();
                        con.Tag = "ZMCA16";

                        Canvas canva = new Canvas() { Height = canvasHeight, Width = 370 };
                        ImageBrush imageBrush = new ImageBrush();
                        imageBrush.ImageSource = new BitmapImage(new Uri("Images/GaugeBack.png", UriKind.Relative));
                        canva.Background = imageBrush;

                        ZMCA16Controler controler = new ZMCA16Controler();
                        RadChart monthChart = new RadChart() { Height = chartHeight, Width = 340 };
                        
                        controler.GetMonthChartData(monthChart);
                        Canvas.SetLeft(monthChart, 10);
                        Canvas.SetTop(monthChart, 35);
                        canva.Children.Add(monthChart);

                        TextBlock title = new TextBlock()
                        {
                            Height = 22,
                            Width = 370,
                            TextAlignment = TextAlignment.Center,
                            FontSize = 16,
                            FontStyle = FontStyles.Normal,
                            FontWeight = FontWeights.Bold
                        };

                        title.Text = "打钻工程进度";
                        Canvas.SetLeft(title, 0);
                        Canvas.SetTop(title, 3);
                        canva.Children.Add(title);

                        //con.Content = engChart;
                        con.Content = canva;

                        c = con;

                        break;
                    }

                default:
                    break;
            }
            c.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            return c;
        }

        private void LoadIssueData()
        {
            Dictionary<string, string> args = new Dictionary<string, string>();
            this.containerProblem.LoadView<EFCustomViewContainerPanel>("ZMCA44", args);
            Dictionary<string, string> arg = new Dictionary<string, string>();
            this.containerIndicate.LoadView<EFCustomViewContainerPanel>("ZMCA43", arg);
        }

        /// <summary>
        /// 由子页面刷新数据
        /// </summary>
        /// <param name="data"></param>
        public override void OnEIDataPublished(EIInfo data)
        {
            Dictionary<string, string> arg = new Dictionary<string, string>();
            this.containerIndicate.LoadView<EFCustomViewContainerPanel>("ZMCA43", arg);
        }

        #region 注册鼠标点击事件

        private void RegEventForGauges()
        {
            foreach (var item in LayoutBasicIndex.Children)
            {
                RegMouseEvent(item as UserControl);
            }
            foreach (var item in LayoutMoreIndex.Children)
            {
                RegMouseEvent(item as UserControl);
            }
        }

        private void RegMouseEvent(UserControl c)
        {
            if (c is SingleCircleGauge)
            {
                RegMouseEventForRadiaGauge((c as SingleCircleGauge).centerGauge);
            }
            else if (c is DoubleCircleGauge)
            {
                RegMouseEventForRadiaGauge((c as DoubleCircleGauge).leftGauge);
                RegMouseEventForRadiaGauge((c as DoubleCircleGauge).rightGauge);
            }
            else if (c is TriCircleGauge)
            {
                RegMouseEventForRadiaGauge((c as TriCircleGauge).leftGauge);
                RegMouseEventForRadiaGauge((c as TriCircleGauge).centerGauge);
                RegMouseEventForRadiaGauge((c as TriCircleGauge).rightGauge);
            }
            else if (c.Content is RadChart)
            {
                RegMouseEventForRadChart(c.Content as RadChart);
            }
            else if (c.Content is Canvas)
            {
                RadChart rc = (c.Content as Canvas).Children[0] as RadChart;
                RegMouseEventForRadChart(rc);
            }

        }

        private void RegMouseEventForRadiaGauge(RadialGauge rg)
        {
            rg.MouseEnter += new MouseEventHandler(gauge_MouseEnter);
            rg.MouseLeave += new MouseEventHandler(gauge_MouseLeave);
            rg.MouseLeftButtonDown += new MouseButtonEventHandler(leftGauge_MouseLeftButtonDown);
        }

        private void RegMouseEventForRadChart(RadChart rg)
        {
            rg.MouseEnter += new MouseEventHandler(gauge_MouseEnter);
            rg.MouseLeave += new MouseEventHandler(gauge_MouseLeave);
            rg.MouseLeftButtonDown += new MouseButtonEventHandler(leftGauge_MouseLeftButtonDown);
        }

        void gauge_MouseLeave(object sender, MouseEventArgs e)
        {
            Cursor = Cursors.Arrow;
        }

        void gauge_MouseEnter(object sender, MouseEventArgs e)
        {
            Cursor = Cursors.Hand;
        }

        void leftGauge_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (sender is RadialGauge)
            {
                object tag = (sender as RadialGauge).Tag == null ?
                    (((sender as RadialGauge).Parent as Canvas).Parent as UserControl).Tag : (sender as RadialGauge).Tag;

                if (tag != null)
                {
                    Common.LoadView(this, tag.ToString());
                }
            }
            else if (sender is RadChart)
            {
                object tag = (sender as RadChart).Tag == null ?
                    (((sender as RadChart).Parent as Canvas).Parent as UserControl).Tag : (sender as RadialGauge).Tag;
                if(tag == null)
                    tag = (sender as RadChart).Tag == null ?
                    ((sender as RadChart).Parent as UserControl).Tag : (sender as RadChart).Tag;

                if (tag != null)
                {
                    Common.LoadView(this, tag.ToString());
                }
            }
        }




        private void imgGetMore_MouseEnter(object sender, MouseEventArgs e)
        {
            imgGetMore.Source = imgGetMoreSource;
        }

        private void imgGetMore_MouseLeave(object sender, MouseEventArgs e)
        {
            imgGetMore.Source = imgGetMoreUnSelectedSource;
        }

        private void imgGetMore_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            LayoutMoreIndex.Visibility = System.Windows.Visibility.Visible;
            imgFoldGetMore.Visibility = System.Windows.Visibility.Visible;
            imgGetMore.Visibility = System.Windows.Visibility.Collapsed;
            spnlGrid.Visibility = System.Windows.Visibility.Collapsed;
            imgGetMore.Source = imgGetMoreUnSelectedSource;
        }

        private void imgFoldGetMore_MouseEnter(object sender, MouseEventArgs e)
        {
            imgFoldGetMore.Source = imgFoldGetMoreSource;
        }

        private void imgFoldGetMore_MouseLeave(object sender, MouseEventArgs e)
        {
            imgFoldGetMore.Source = imgFoldGetMoreUnSelectedSource;
        }

        private void imgFoldGetMore_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            LayoutMoreIndex.Visibility = System.Windows.Visibility.Collapsed;
            imgFoldGetMore.Visibility = System.Windows.Visibility.Collapsed;
            imgGetMore.Visibility = System.Windows.Visibility.Visible;
            spnlGrid.Visibility = System.Windows.Visibility.Visible;
            imgFoldGetMore.Source = imgFoldGetMoreUnSelectedSource;
        }
        #endregion


        void AddGaugeToGrid(Grid grid)
        {
            StackPanel stack = new StackPanel()
            {
                Orientation = Orientation.Horizontal,
                HorizontalAlignment = HorizontalAlignment.Stretch
            };

            SingleCircleGauge gauge1 = new SingleCircleGauge();
            //gauge1.MouseLeftButtonDown += new MouseButtonEventHandler(gauge1_MouseLeftButtonDown);
            SingleCircleGauge gauge2 = new SingleCircleGauge();

            stack.Children.Add(gauge1);
            stack.Children.Add(gauge2);
            //stack.SetValue(Grid.RowProperty, rowIndex);
            //stack.SetValue(Grid.ColumnProperty, colIndex++);
            grid.Children.Add(stack);
        }

        public void InitTrendChart(RadChart TrendChart, int type)
        {
            Common.ClearChart(TrendChart);
            //趋势图
            TrendChart.SeriesMappings.Clear();
            TrendChart.DefaultSeriesDefinition.ShowItemToolTips = false;

            TrendChart.DefaultSeriesDefinition.LegendDisplayMode = LegendDisplayMode.None;
            TrendChart.DefaultView.ChartLegend.Visibility = System.Windows.Visibility.Collapsed;
            TrendChart.DefaultView.ChartLegend.Header = "";

            TrendChart.DefaultView.ChartArea.FontSize = 10;
            TrendChart.DefaultView.ChartArea.AxisY.AutoRange = true;
            TrendChart.DefaultView.ChartArea.AxisY.MinValue = 0;
            TrendChart.DefaultView.ChartArea.AxisY.MaxValue = 10000;
            TrendChart.DefaultView.ChartArea.AxisY.Step = 0.2;
            TrendChart.DefaultView.ChartArea.AxisY.DefaultLabelFormat = "#VAL ";
            TrendChart.DefaultView.ChartArea.AxisY.MajorGridLinesVisibility = Visibility.Visible;
            TrendChart.DefaultView.ChartArea.AxisY.MinorGridLinesVisibility = Visibility.Visible;
            TrendChart.DefaultView.ChartArea.AxisY.StripLinesVisibility = Visibility.Visible;

            TrendChart.DefaultView.ChartTitle.FontSize = 14;

            SeriesMapping smBar = new SeriesMapping();
            switch (type)
            {
                case 0:
                    TrendChart.DefaultView.ChartTitle.Content = "采煤产量分析";
                    smBar.SeriesDefinition = new BarSeriesDefinition();
                    break;
                case 1:
                    TrendChart.DefaultView.ChartTitle.Content = "掘进进度分析";
                    smBar.SeriesDefinition = new LineSeriesDefinition();
                    break;
                case 2:
                    //TrendChart.DefaultView.ChartTitle.Content = DateTime.Now.Month + "月防突打钻进度";
                    smBar.SeriesDefinition = new SplineSeriesDefinition();
                    break;
                default:
                    smBar.SeriesDefinition = new BarSeriesDefinition();
                    break;
            }

            TrendChart.DefaultView.ChartTitle.Visibility = Visibility.Collapsed;

            smBar.SeriesDefinition.ShowItemLabels = false;
            smBar.SeriesDefinition.ShowItemToolTips = true;
            smBar.SeriesDefinition.ShowZeroValuesLabels = true;
            smBar.CollectionIndex = 0;
            smBar.LegendLabel = "数据总量";
            smBar.ItemMappings.Add(new ItemMapping("Date", DataPointMember.XCategory));
            smBar.ItemMappings.Add(new ItemMapping("DataValue", DataPointMember.YValue));

            smBar.SeriesDefinition.InteractivitySettings.HoverScope = InteractivityScope.Series;
            smBar.SeriesDefinition.InteractivitySettings.SelectionScope = InteractivityScope.Series;
            smBar.SeriesDefinition.InteractivitySettings.SelectionMode = ChartSelectionMode.Multiple;
            //series.SeriesDefinition.LegendDisplayMode = LegendDisplayMode.DataPointLabel;
            TrendChart.SeriesMappings.Add(smBar);
        }






    }
}
